<?php

/**
 * This is the model class for table "zy_user_service".
 *
 * The followings are the available columns in table 'zy_user_service':
 * @property integer $user_service_id
 * @property integer $user_id
 * @property integer $service_id
 * @property string $valid_date
 * @property integer $status
 * @property integer $equipment_id
 * @property integer $package_id
 * @property integer $create_user
 * @property integer $access_id
 * @property string $equipment_sell_price
 * @property string $equipment_rent_price
 * @property string $package_price
 * @property string $deposit
 */
class UserService extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserService the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_user_service';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id,service_id,valid_date,status', 'required'),
			array('user_id, service_id, status, equipment_id, package_id, create_user,access_id', 'numerical', 'integerOnly'=>true),
			array('deposit', 'numerical', 'integerOnly'=>false),
			array('equipment_sell_price, equipment_rent_price, package_price,equipment_number', 'length', 'max'=>255),
			array('valid_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('equipment_number,user_service_id, user_id, service_id, valid_date, status, equipment_id, package_id, create_user, access_id, equipment_sell_price, equipment_rent_price, package_price, deposit', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'user'=>array(self::BELONGS_TO,'User','user_id'),
				'equipment'=>array(self::BELONGS_TO,'Equipment','equipment_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_service_id' => 'User_Service_id',
			'user_id' => Yii::t('base','Username'),
			'service_id' => Yii::t('base','Servicename'),
			'valid_date' => Yii::t('base','ValidDate'),
			'status' => Yii::t('base','Status'),
			'package_id'=>Yii::t('base','Package Name'),
			'equipment_id'=>Yii::t('base','Equipment Name'),
			'equipment_sell_price'=>Yii::t('base','Equipment Sell Price'),
			'equipment_rent_price'=>Yii::t('base','Equipment Rent Price'),
			'package_price'=>Yii::t('base','Package Price'),
			'deposit'=>Yii::t('base','Deposit'),
			'equipment_number'=>Yii::t('base','equipment_number'),
			'isreback'=>Yii::t('base','Isreback'),
		);
	}
	

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($results)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->compare('user_service_id',$this->user_service_id);
		$criteria->compare('user_id',User::model()->getUserId($this->user_id));
		$criteria->addInCondition('user_id',$results);
		$criteria->compare('service_id',Service::model()->getServiceId($this->service_id));
		$criteria->compare('valid_date',$this->valid_date,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('equipment_id',$this->equipment_id);
		$criteria->compare('package_id',$this->package_id);
		$criteria->compare('create_user',$this->create_user);
		$criteria->compare('access_id',$this->access_id);
		$criteria->compare('equipment_sell_price',$this->equipment_sell_price,true);
		$criteria->compare('equipment_rent_price',$this->equipment_rent_price,true);
		$criteria->compare('package_price',$this->package_price,true);
		$criteria->compare('deposit',$this->deposit,true);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'user_service_id DESC',
				),
		));
	}
	public function search2($user_id)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
		if($user_id>0){
			$criteria->addCondition("user_id=".$user_id);
		}
		$criteria->compare('user_service_id',$this->user_service_id);
		$criteria->compare('user_id',User::model()->getUserId($this->user_id));
		$criteria->compare('service_id',Service::model()->getServiceId($this->service_id));
		$criteria->compare('valid_date',$this->valid_date,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('equipment_id',$this->equipment_id);
		$criteria->compare('package_id',$this->package_id);
		$criteria->compare('create_user',$this->create_user);
		$criteria->compare('access_id',$this->access_id);
		$criteria->compare('equipment_sell_price',$this->equipment_sell_price,true);
		$criteria->compare('equipment_rent_price',$this->equipment_rent_price,true);
		$criteria->compare('package_price',$this->package_price,true);
		$criteria->compare('deposit',$this->deposit,true);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'user_service_id DESC',
				),
		));
	}
	
	public function getUserServiceId()
	
	{
		$id = yii::app()->user->id;
	
		$returnArr = UserService::model()->findAll('user_id='.$id);
	
		return CHtml::listData($returnArr,'service_id','service_id');
	
	}
	public function getServiceName($user_service_id)
	{
		$userservice = UserService::model()->findByPk($user_service_id);
		if($userservice->service_id){
			$service = Service::model()->find('service_id='.$userservice->service_id);
			return $service->service_name;
		}
	}
	//获得总帐号下的服务
	public function getTopServiceName()
	{
		$id = yii::app()->user->id;
		$sql = "SELECT * FROM zy_service AS a LEFT JOIN  zy_user_service AS b  ON a.`service_id` = b.`service_id` WHERE b.`user_id`=".$id;
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($rs,'service_id','service_name');
	}
	// 获取状态 -- 1.正在使用  2.停用
	public function getStatus($id)
	{
		$userservice = UserService::model()->findByPk($id);
		if($userservice->status==0){
			$userservice->status = Yii::t('base','disable');
		}elseif($userservice->status==1){
			$userservice->status = Yii::t('base','ACTIVE');
		}
		return $userservice->status;
	}
	//update的时候特定的用户名显示
	public function getuser($id)
	{
		$sql="select * from zy_user as a left join  zy_user_service as b on a.user_id = b.user_id where b.user_id =".$id;
		//echo $sql;die();
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
	
		return CHtml::listData($rs,'user_id','user_name');
	}
	//update的时候特定的服务名显示
	public function getservice($id)
	{
		$sql = "select * from zy_service as a left join zy_user_service as b on a.service_id = b.service_id where b.service_id = ".$id;
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
	
		return CHtml::listData($rs,'service_id','service_name');
	}
	public function getPackageName()
	{
		$rs = Package::model()->findAll();
		return CHtml::listData($rs,'package_id','package_name');
	}
	public function getEquipmentName()
	{
		$rs = Equipment::model()->findAll();
		return CHtml::listData($rs,'equipment_id','equipment_name');
	}
	public function getAccessEquipmentNumber()
	{	
		$id=yii::app()->user->id;
		$sql = "select a.* from zy_equipment as a left join zy_user_service as b on a.equipment_id = b.equipment_id where b.access_id='".$id."' and b.isreback =1 and a.is_used = 1 and b.user_id=0";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($rs,'equipment_number','equipment_number');
	}
	public function AgentDistribution(){
		$access_id = $_POST['access_id'];
		$equipment = $_POST['equipment'];
		//$city=$_POST['city'];
		for($i=0;$i<count($equipment);$i++) {
			$equipment_id = $equipment[$i];
			$rs=Equipment::model()->findByPk($equipment_id);
			// is_used=1 表示设备不在admin里面 istop=0表示是admin非分配给代理人的=1 表示是代理人分配给下面的代理人的
			$rs->is_used=1;
			$rs->istop=0;
			$rs->updater=$access_id;
			$rs->update();
		}
		return true;
	}
	public function StaffDistribution(){
		$access_id = $_POST['access_id'];
		$equipment = $_POST['equipment'];
		for($i=0;$i<count($equipment);$i++) {
			$equipment_id = $equipment[$i];
			$rs=Equipment::model()->findByPk($equipment_id);
			$rs->is_used=1;
			$rs->istop=1; //istop=1表示是总代理给职工代理
			$rs->updater=$access_id;
			$rs->update();
		}
		return true;
	}
	/**
	 * 获取设备名称
	 */
	public function getOneEquipmentName($id){
		$sql="select equipment_number from zy_equipment where equipment_id=".$id;
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return $rs[0]['equipment_number'];
	}
}